import 'package:flutter_base_plugin/base_library/base_interfaces/i_app_config.dart';
import 'package:flutter_base_plugin/base_library/base_interfaces/i_http_bean_config.dart';

import '../base_bugly/flutter_bugly.dart';
import '../xlog_utils/xlog_utils.dart';
import 'base_config/bugly_config.dart';
import 'base_const/constants.dart';
import 'base_interfaces/i_http_config.dart';
import 'base_interfaces/i_load_config.dart';
import 'base_config/log_config.dart';
import 'base_interfaces/i_toast.dart';
import 'base_network/x_api.dart';
import 'base_utils/toast_utils.dart';

class BasePlugin {
  BasePlugin._();

  ///是否是debug模式
  static bool _isDebug = true;

  static bool get isDebug => _isDebug;

  ///全局资源配置类
  static late ILoadConfig _loadConfig;

  static ILoadConfig get loadConfig => _loadConfig;

  ///全局资源配置类
  static late IAppConfig _appConfig;

  static IAppConfig get appConfig => _appConfig;

  ///初始化基础库,应用启动时调用
  ///[isDebug] 是否是debug模式
  ///[logConfig] 日志库配置
  ///[httpConfig] http相关配置信息
  ///[resConfig] 全局通用资源配置
  ///[buglyConfig] bugly相关配置信息，为null不使用bugly
  ///[toastImpl] Toast相关配置，为null使用默认Toast样式
  static Future<void> init({
    required bool isDebug,
    required IAppConfig appConfig,
    required IHttpConfig httpConfig,
    required IHttpBean httpBean,
    required ILoadConfig loadConfig,
    required LogConfig logConfig,
    BuglyConfig? buglyConfig,
    IToast? toastImpl,
  }) async {
    _isDebug = isDebug;
    _loadConfig = loadConfig;
    _appConfig = appConfig;

    Constants.initPageIndex = httpConfig.configInitPageIndex();
    Constants.pageSize = httpConfig.configPageSize();

    ///初始化bugly
    if (buglyConfig != null) {
      await FlutterBugly.init(
        androidAppId: buglyConfig.androidAppId,
        iOSAppId: buglyConfig.iOSAppId,
        channel: buglyConfig.channel,
        autoCheckUpgrade: buglyConfig.autoCheckUpgrade,
        autoInit: buglyConfig.autoInit,
        autoDownloadOnWifi: buglyConfig.autoDownloadOnWifi,
        enableHotfix: buglyConfig.enableHotfix,
        enableNotification: buglyConfig.enableNotification,
        showInterruptedStrategy: buglyConfig.showInterruptedStrategy,
        canShowApkInfo: buglyConfig.canShowApkInfo,
        initDelay: buglyConfig.initDelay,
        upgradeCheckPeriod: buglyConfig.upgradeCheckPeriod,
        checkUpgradeCount: buglyConfig.checkUpgradeCount,
        customUpgrade: buglyConfig.customUpgrade,
      );
    }

    ///初始化日志库
    XLogUtils.init(
      isDebug: logConfig.isDebug,
      tag: logConfig.tag,
    );

    ///初始化Toast
    ToastUtils.init(toastImpl);

    ///初始化http组件
    XApi.init(httpConfig, httpBean);
  }
}
